package com.hanhai.zrb.api.biz.dao.mybatis;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.hanhai.zrb.api.biz.dao.mybatis.base.BaseMapper;
import com.hanhai.zrb.model.common.CommonCounter;
import com.hanhai.zrb.model.common.CommonCounterKey;

public interface CommonCounterMapper extends BaseMapper{

	@Insert("insert into common_counter (main_key, secondary_key, value)values (#{mainKey}, #{secondaryKey}, #{value})")
    int insert(CommonCounter record);

	@Update("update common_counter set value = value +1 where main_key = #{mainKey} and secondary_key = #{secondaryKey}")
    int addOne(@Param("mainKey") String mainKey,@Param("secondaryKey")String secondaryKey);
    
	@Update("update common_counter set value = value -1 where main_key = #{mainKey} and secondary_key = #{secondaryKey} and value > 0")
    int subOne(@Param("mainKey") String mainKey,@Param("secondaryKey")String secondaryKey);
    
	@Update("update common_counter set value = value +1 where main_key = #{mainKey} and secondary_key = #{secondaryKey} and value < #{maxValue}")
    int addOneForMaxValue(@Param("mainKey") String mainKey,@Param("secondaryKey")String secondaryKey,Long maxValue);
	
    @Select("select value from common_counter where main_key = #{mainKey} and secondary_key = #{secondaryKey}")
    Long getValueByKey(@Param("mainKey") String mainKey,@Param("secondaryKey") String secondaryKey);
    
    CommonCounter getByKey(@Param("mainKey") String mainKey,@Param("secondaryKey") String secondaryKey);
    
    List<CommonCounter> getListBykeyList(List<CommonCounterKey> list);
    
    List<CommonCounter> getListBySecondekeyList(@Param("mainKey") String mainKey,@Param("secondaryKeys") List<String> secondaryKeys);
    
    CommonCounter selectForUpdate(@Param("mainKey") String mainKey,@Param("secondaryKey") String secondaryKey);
    
    @Update("update common_counter set value = #{value} where main_key = #{mainKey} and secondary_key = #{secondaryKey}")
    public int update(CommonCounter record);
    
    List<CommonCounter> getListByMainKey(@Param("mainKey") String mainKey);
}